package com.myrabbitmqdemo.demo.three;

import com.myrabbitmqdemo.demo.utils.RabbitMqUtils;
import com.myrabbitmqdemo.demo.utils.SleepUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 * 处理时间较长的那种
 */
public class Work04 {

    private static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = RabbitMqUtils.getChannel();

        System.out.println("C2等待接收消息处理时间较长");
        DeliverCallback deliverCallback = (consumerTag,message) -> {
            SleepUtils.sleep(30);
            System.out.println("接收到的消息："+new String(message.getBody(), StandardCharsets.UTF_8));
            /**
             * 手动应答
             * 1、该消息的tag
             * 2、是否批量处理
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        // 不公平分发这个prefetchCount这个等于0是轮询分发，等于1是不公平分发，等于大于1的是预取值分发
//        int prefetchCount = 1;
        int prefetchCount = 5;
        //  不公平分发设置
        channel.basicQos(prefetchCount);

        channel.basicConsume(TASK_QUEUE_NAME,false,deliverCallback,(consumerTag)->{
            System.out.println(consumerTag+":消费者");
        });
    }
}
